// stdx/memx.rs
// CRITICAL: std::mem 相关示例（size_of, take, replace）。
// AUTO_COMMENT: 小心使用 mem::replace/take，避免未初始化场景。

#[allow(dead_code)]
pub fn stdx_examples_memx() {
    use std::mem;
    let x: i32 = 5;
    let _size = mem::size_of_val(&x);

    let mut v = Some(3);
    let old = mem::take(&mut v);
    let _ = old;

    // CRITICAL: size_of 和 size_of_val
    let size_i32 = mem::size_of::<i32>();
    let size_vec = mem::size_of_val(&vec![1, 2, 3]);

    // CRITICAL: align_of 和 align_of_val
    let align_i32 = mem::align_of::<i32>();
    let align_vec = mem::align_of_val(&vec![1, 2, 3]);

    // CRITICAL: replace 和 swap
    let mut a = 1;
    let old_a = mem::replace(&mut a, 2);
    let mut b = 3;
    mem::swap(&mut a, &mut b);

    // CRITICAL: transmute（危险，需谨慎使用）
    let float: f32 = 1.0;
    let int: u32 = unsafe { mem::transmute(float) };

    // CRITICAL: discriminant 用于枚举
    #[derive(Debug)]
    enum MyEnum {
        A(i32),
        B(f64),
    }
    let e1 = MyEnum::A(1);
    let e2 = MyEnum::B(2.0);
    let disc1 = mem::discriminant(&e1);
    let disc2 = mem::discriminant(&e2);

    // CRITICAL: forget（防止析构）
    let s = String::from("hello");
    mem::forget(s); // s 不会被 drop

    let _ = (
        size_i32, size_vec, align_i32, align_vec, old_a, a, b, int, disc1, disc2,
    );
}
